package com.example.rxpermissiondemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;


public class SplashActivity extends Activity {
    private static final String TAG = "SplashActivity";
    private Activity mActivity = SplashActivity.this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        // 判断必须授予的权限是否都授予了
        if (!PermissionManager.getInstance().hasPermisson(this, PermissionManager.MUST_GRANTED_PRIMISSION)) {
            // 没有权限，延时一秒，申请权限，方便用户看到启动页
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    PermissionSplashDialog dialog = new PermissionSplashDialog(SplashActivity.this, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 申请权限
                            PermissionManager.getInstance().requestPermissionWithEssential(SplashActivity.this, new PermissionManager.CallBackListener() {
                                @Override
                                public void granted() {
                                    // 授予了权限，则进入首页
                                    goMain();
                                }

                                @Override
                                public void refused() {

                                }

                                @Override
                                public void noLongerRemind(String... permissionNames) {

                                }
                            }, PermissionManager.MUST_GRANTED_PRIMISSION);
                        }
                    });
                    dialog.show();
                }
            }, 1000);
        } else {
            // 有权限，则直接进入首页
            goMain();
        }

    }

    /**
     * 去首页
     */
    private void goMain() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
        }, 2000);
    }
}

